package com.l024.android_ljl.view.main.activity;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.R;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivitySearchBinding;
import com.l024.android_ljl.entity.MessageWrap;
import com.l024.android_ljl.entity.SysOptionEntity;
import com.l024.android_ljl.entity.SysSubjectEntity;
import com.l024.android_ljl.viewmodel.main.SearchViewModel;
import com.quantstack.llcommonlibrary.adapter.CommonAdapter;
import com.quantstack.llcommonlibrary.adapter.CommonViewHolder;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity<SearchViewModel, ActivitySearchBinding> implements BaseViewModel.OnPageDataListener {
    private static final int UPDATE_LIST = 2001;
    private List<SysSubjectEntity> list = new ArrayList<>();
    private CommonAdapter<SysSubjectEntity> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;

    }

    @Override
    public void afterCreate() {
        initList();
        EventBus.getDefault().register(this);
        mViewModel.setBaseView(this);
        mViewModel.setOnPageDataListener(this);
        mViewDataBind.setModel(mViewModel);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageWrap message) {
        if(message!=null){
            switch (message.getCode()){
                case UPDATE_LIST:{
                    //更新选项
                    refreshList();
                    break;
                }
            }
        }
    }

    private void initList() {
        adapter = new CommonAdapter<>(list, new CommonAdapter.OnBindDataListener<SysSubjectEntity>() {
            @Override
            public void onBindViewHolder(SysSubjectEntity subject, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.tv_title,subject.getTitle());
                viewHolder.setText(R.id.tv_answer,"答案:"+subject.getAnswer());
                viewHolder.setText(R.id.tv_note,"解析:"+subject.getNote());
                LinearLayout llOptions = viewHolder.getView(R.id.ll_options);
                List<SysOptionEntity> options = subject.getOptions();
                if(options!=null&&options.size()>0){
                    llOptions.removeAllViews();
                    for(SysOptionEntity optionEntity:options){
                        TextView view = new TextView(SearchActivity.this);
                        view.setText(optionEntity.getContent());
                        view.setTextSize(18);
                        view.setTextColor(getResources().getColor(R.color.c333));
                        llOptions.addView(view);
                    }
                }
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.item_search;
            }
        });
        mViewDataBind.rlSubject.setLayoutManager(new LinearLayoutManager(this));
        mViewDataBind.rlSubject.setAdapter(adapter);
    }

    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.SEARCH_SUBJECT:{
                refreshList();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.SEARCH_SUBJECT:{
                ToastUtils.show(msg);
                break;
            }
        }
    }

    private void refreshList(){
        list.clear();
        List<SysSubjectEntity> value = mViewModel.getSubjects().getValue();
        list.addAll(value);
        adapter.notifyDataSetChanged();
    }
}